package thinkingInJava.containers;

//: containers/SlowMap.java
//A Map implemented with ArrayLists.
import java.util.*;

public class SlowMap<K,V> extends AbstractMap<K,V> {
private List<K> keys = new ArrayList<K>();
private List<V> values = new ArrayList<V>();
private SlowMapSet enSet = new SlowMapSet();

// 为了保证 entrySet 在外面被改时，同步这里的 keys values
private class SlowMapSet extends HashSet<Map.Entry<K,V>>{
	@Override
	public boolean removeAll(Collection<?> c){
		boolean isRemoved = false;
		 Iterator<K> ki = keys.iterator();
		 while(ki.hasNext())
		 {
			 K k = ki.next();
			 if( c.contains(k) )
			 {
				 SlowMap.this.remove(k);
				 isRemoved = true;
			 }
		 }
		//super.removeAll(c);
		 return isRemoved;
	}
	public boolean removeAllSuper(Collection<?> c){
		return super.remove(c);
	}
	
	@Override
	public boolean remove(Object o){
		return null!=SlowMap.this.remove(o);
	}
	public boolean removeSuper(Object o){
		return super.remove(o);
	}
	
	@Override
	public void clear(){
		SlowMap.this.clear();
	}
	public void clearSuper(){
		super.clear();
	}
	// 实际上所有会改变的都要重载以使内外一致
//	@Override
//	public boolean add(Entry<K,V>){
//		
//	}
}

public V put(K key, V value) {
 V oldValue = get(key); // The old value or null
 if(!keys.contains(key)) {
   keys.add(key);
   values.add(value);
   // 保证 set 的正确性
   enSet.add(new MapEntry<K,V>(key, value));
 } else{
	   values.set(keys.indexOf(key), value);
	   // 保证 set 的正确性
	   Iterator<Map.Entry<K,V>> it = enSet.iterator();
	   while(it.hasNext())
	   {
		   Map.Entry<K,V> en = it.next();
		   if( en.getKey().equals(key) )
		   {
			   en.setValue(value);
			   break;
		   }
	   } 
 }
 return oldValue;
}
public V get(Object key) { // key is type Object, not K
 if(!keys.contains(key))
   return null;
 return values.get(keys.indexOf(key));
}
public Set<Map.Entry<K,V>> entrySet() {
	return enSet;
// Set<Map.Entry<K,V>> set= new HashSet<Map.Entry<K,V>>();
// Iterator<K> ki = keys.iterator();
// Iterator<V> vi = values.iterator();
// while(ki.hasNext())
//   set.add(new MapEntry<K,V>(ki.next(), vi.next()));
// return set;
}
////////////////////////////// 自己加上去的 //////////////////////////////////
public V remove(Object key){
	if(!keys.contains(key))
		  return null;
	
	 for(int i=0; i<keys.size(); i++)
	   if( keys.get(i).equals(key) )
	   {
		   keys.remove(i);
		   // 保证 set 的正确性
		   enSet.removeSuper(key);
		   return values.remove(i);
	   }

	 return null;
}

public void clear(){
	keys.clear();
	values.clear();
	enSet.clearSuper();
}

public boolean isEmpty(){
	return keys.isEmpty();
}

public static void main(String[] args) {
 SlowMap<String,String> m= new SlowMap<String,String>();
 m.putAll(Countries.capitals(15));
 System.out.println(m);
 System.out.println(m.get("BULGARIA"));
 System.out.println(m.entrySet());
}
} /* Output:
{CAMEROON=Yaounde, CHAD=N'djamena, CONGO=Brazzaville, CAPE VERDE=Praia, ALGERIA=Algiers, COMOROS=Moroni, CENTRAL AFRICAN REPUBLIC=Bangui, BOTSWANA=Gaberone, BURUNDI=Bujumbura, BENIN=Porto-Novo, BULGARIA=Sofia, EGYPT=Cairo, ANGOLA=Luanda, BURKINA FASO=Ouagadougou, DJIBOUTI=Dijibouti}
Sofia
[CAMEROON=Yaounde, CHAD=N'djamena, CONGO=Brazzaville, CAPE VERDE=Praia, ALGERIA=Algiers, COMOROS=Moroni, CENTRAL AFRICAN REPUBLIC=Bangui, BOTSWANA=Gaberone, BURUNDI=Bujumbura, BENIN=Porto-Novo, BULGARIA=Sofia, EGYPT=Cairo, ANGOLA=Luanda, BURKINA FASO=Ouagadougou, DJIBOUTI=Dijibouti]
*///:~

